<?php
/**
 * admin模块基础类
 * User: ws
 * Date: 2018/7/20
 * Time: 14:39
 */

namespace app\admin\controller;
use think\Controller;
use app\admin\model\AdminMenu as MenuModel;
use think\Db;
class Init  extends Controller
{

    function _initialize()
    {
        parent::_initialize();
        error_reporting(0);
        $this->model = model('admin/admin_user');
        $this->log = model('admin/log');
        if(!session('?admin_user') && strtolower(request()->controller()) != 'login'){
            $this->redirect('login/login');
        }
        $this->settings = cache('settings');

        // 发送基本信息
        $this->assign(['settings' => $this->settings,'admin_user' => session('admin_user')]);

        // 有用户则读取菜单
        if (session('?admin_user')) {
            $this->assign('_admin_menu', MenuModel::getMainMenu());
        }
        if(!empty($_SESSION['think']['admin_user'])){
            //写入日志
            $method = $this->getMethod();
            if($method=='index' || $method=='order_details'){
                $data_log['title'] = '浏览数据';
            }elseif ($method=='add'){
                $data_log['title'] = '添加数据';
            }elseif ($method=='edit'){
                $data_log['title'] = '更新数据';
            }elseif ($method=='del'){
                $data_log['title'] = '删除数据';
            }elseif ($method=='home'){
                $data_log['title'] = '登录';
            }elseif ($method=='logout'){
                $data_log['title'] = '退出';
            }elseif ($method=='upimage_s'){
                $data_log['title'] = '上传文件';
            }elseif ($method=='uploadFiles'){
                $data_log['title'] = '上传文件';
            }elseif ($method=='state'){
                $data_log['title'] = '状态';
            }elseif ($method=='sort'){
                $data_log['title'] = '排序';
            }else{
                $data_log['title'] = '其它';
            }
            $param = $this->request->post();
            if(!empty($param)){
                $data_log['param'] = json_encode($param);
            }

            $data_log['a_id'] = $_SESSION['think']['admin_user']['admin_user_id'];
            $data_log['type'] = 1;
            $data_log['ip'] = '';
            $data_log['url'] = $this->getUrl();
            if($method!='null'){
                $this->log->add($data_log);
            }
        }

    }

    function state() {
        $val   = input('param.val');
        $ids   = input('param.ids/a') ? input('param.ids/a') : input('param.id/a');
        $table = input('param.table');
        $field = input('param.field', 'state');
        if($table=='goods'){
            $field = input('param.field', 'shelves');
        }
        if(isset($_GET['type'])==true){
            $field = input('param.field', 'recommend');
        }
        if (empty($ids)) {
            return $this->error('参数传递错误[1]！');
        }
        if (empty($table)) {
            return $this->error('参数传递错误[2]！');
        }
        // 以下表操作需排除值为1的数据
        if ($table == 'admin_user') {
            if (in_array('1', $ids)) {
                return $this->error('系统限制操作');
            }
        }
        // 获取主键
        $pk = Db::name($table)->getPk();
        $map = [];
        $map[$pk] = ['in', $ids];
        $res = Db::name($table)->where($map)->setField($field, $val);
        if ($res === false) {
            return $this->error('状态设置失败');
        }
        return $this->success('状态设置成功');
    }

    function sort() {
        $ids   = input('param.ids/d') ? input('param.ids/d') : input('param.id/d');
        $table = input('param.table');
        $field = input('param.field/s', 'sort');
        $val   = input('param.val/d');
        // 获取主键
        $pk = Db::name($table)->getPk();
        $map = [];
        $map[$pk] = ['in', $ids];
        $res = Db::name($table)->where($map)->setField($field, $val);
        if ($res === false) {
            return $this->error('排序设置失败');
        }
        return $this->success('排序设置成功');
    }

    //获取操作路径
    protected function getUrl() {
        $model      = request()->module();
        $controller = request()->controller();
        $action     = request()->action();
        return $model.'/'.$controller.'/'.$action;
    }

    //获取方法，用户分辨是添加还是更新操作
    protected function getMethod() {
        return request()->action();
    }
}